package com.candoit.webapps.pricing.controllers;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import com.candoit.webapps.pricing.model.Formato;
import com.candoit.webapps.pricing.model.Linea;
import com.candoit.webapps.pricing.model.MateriaPrima;
import com.candoit.webapps.pricing.util.LineaUtil;
import com.candoit.webapps.pricing.util.QueryUtil;

@ManagedBean(name = "lineaCtrl")
@SessionScoped
public class LineaController extends AbstractController {
	
	private List<LineaUtil> lineas;
	
	private Linea lineaSelected;
	
	private List<MateriaPrima> materias; 
	
	public List<LineaUtil> getLineas() {
		List<Linea> lines = QueryUtil.getInstance().getLineas();
		this.fillLineas(lines);
		return lineas;
	}

	public void setLineas(List<LineaUtil> lineas) {
		this.lineas = lineas;
	}

	public Linea getLineaSelected() {
		if (lineaSelected == null){
			lineaSelected = new Linea();
		}
		return lineaSelected;
	}

	public void setLineaSelected(Linea lineaSelected) {
		this.lineaSelected = lineaSelected;
	}

	public void saveLinea(){
		Iterator<LineaUtil> it = lineas.iterator();
		while (it.hasNext()){
			   LineaUtil util = (LineaUtil) it.next();
				   if (util.getLinea().getId() == null || util.getLinea().getId() == 0){
					    if (util.getLinea().getNombre() != null && !util.getLinea().getNombre().equals("")){	
					   			this.save(util.getLinea());
					    }	
				   }else{
					   this.update(util.getLinea());
				   }
			   }
	}
	
	public void removeLinea(){
		Iterator<LineaUtil> it = lineas.iterator();
		while (it.hasNext()){
			   LineaUtil util = (LineaUtil) it.next();
			   if (util.isSelected()){
				   if (util.getLinea().getId() != null && util.getLinea().getId() != 0){
					   List<Formato> formatos = QueryUtil.getInstance().getFormatosByIdLinea(util.getLinea().getId());
					   for (Formato formato : formatos){
						    formato.setLinea(null);
						    this.update(formato);
					   }
					   this.remove(util.getLinea());
				   }
			   }
		}
	}
	
	public void clearData(){
		this.saveSuccess = false;
		this.deleteSuccess = false;
		this.lineaSelected = null;
	}
	
	private boolean validate(){
		boolean validate = true;
		if (lineaSelected.getNombre() == null || lineaSelected.getNombre().equals("")){
			FacesContext.getCurrentInstance().addMessage("savelineaform:nombre", new FacesMessage(NAME_LINE_EMPTY));
			validate = false;
		}
		if (lineaSelected.getInicial() == null || lineaSelected.getInicial().equals("")){
			FacesContext.getCurrentInstance().addMessage("savelineaform:inicial", new FacesMessage(NAME_LINE_EMPTY));
			validate = false;
		}
		
		return validate;
	}
	
	private void fillLineas(List<Linea> lines){
		lineas = new ArrayList<LineaUtil>();
		Iterator<Linea> it = lines.iterator();
		while (it.hasNext()){
			   Linea linea = (Linea) it.next();
			   LineaUtil util = new LineaUtil();
			   util.setLinea(linea);
			   util.setSelected(false);
			   lineas.add(util);
		}
		
		LineaUtil utilLinea = new LineaUtil();
		Linea linea = new Linea();
		utilLinea.setLinea(linea);
		utilLinea.setSelected(false);
		lineas.add(utilLinea);
	}

	public List<MateriaPrima> getMaterias() {
		if (materias == null){
			materias = QueryUtil.getInstance().getMateriasPrimas();
		}
		return materias;
	}

	public void setMaterias(List<MateriaPrima> materias) {
		this.materias = materias;
	}
	
	public void saveMaterias(){
		Integer size = lineaSelected.getMaterias().size();
		this.update(lineaSelected);
	}
	
}
